<template>
  <div class="common-layout">
    <el-container>
      <el-header>
        <div class="demo-type">
          <el-avatar :icon="UserFilled" />
        </div>
        <el-dropdown>
          <span class="el-dropdown-link">
            {{ usermessage }}
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item>账号设置</el-dropdown-item>
              <el-dropdown-item>切换账号</el-dropdown-item>
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>

      <el-main>
        <div class="serch_box">
          <el-autocomplete v-model="state2" :fetch-suggestions="querySearch" :trigger-on-focus="false" clearable
          
            class="inline-input w-50" placeholder="Please Input" @select="handleSelect" 
            style="width:70%"
            />

          
          <div class="right-items" style="float:right">
            <el-button type="primary" :icon="Search">Search</el-button>
          </div>
        </div>

      </el-main>
    </el-container>
  </div>
</template>

<script setup lang='ts'>
import { getUserSimplifyMessage } from '../../api/common'
import { getAreaSimplifyName } from './../../api/area'
import type { Areas } from "../../api/types/common";
import { UserFilled } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { Search } from '@element-plus/icons-vue'



onMounted(() => {

  getusermessage()
  getAreamessage()
})


interface RestaurantItem {
  value: string;
  place: string;
  link: {
    herf: string
    method: string
  }

}
const list = ref<Areas[]>([])
const restaurants = ref<RestaurantItem[]>([])
const state2 = ref('')
const usermessage = ref('')
// 获取用户的用户名

const getusermessage = async () => {

  const username = await getUserSimplifyMessage()
  usermessage.value = username.data.data.userMessage.userName


}


const querySearch = (queryString: string, cb: any) => {
  console.log(queryString)
  const results = queryString
    ? restaurants.value.filter(createFilter(queryString))
    : restaurants.value
  // call callback function to return suggestions
  cb(results)
}
const createFilter = (queryString: string) => {
  return (restaurant: RestaurantItem) => {
    return (
      restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
    )
  }

}

const getAreamessage = async () => {
  const userdata = await getAreaSimplifyName()
  console.log(userdata)
  list.value = userdata.areas
  console.log(userdata.areas)
  console.log('zheg', list.value)

  var datalist = list.value.map((item) => {
    return {
      value: item.name,
      place: item.place,
      link: item.link
    }
  })
  console.log(datalist)
  restaurants.value = datalist



}

const handleSelect = (item: RestaurantItem) => {
  console.log(item)
}



</script>




<style scoped>
.common-layout{
    display: flex;
     height: 100vh;

   
  
}

.demo-type {
  display: flex;
  position: relative;

  
}
 .el-header {
  background-color: #292929e6;
  --el-header-height: 10%;;
}
 .el-main {
  
    text-align: center;
        display: flex;
    justify-content: center;
    align-items: center;


  }   

.demo-type>div {
  flex: 1;
  text-align: center;
}

.demo-type>div:not(:last-child) {
  border-right: 1px solid var(--el-border-color);
}

.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}


.demo-type[data-v-0d0c7376] {
    display: flex;
    position: relative;
    margin-top: 1%;
    margin-left: 95%;
}

.el-dropdown {
  color: white;
  margin-top: -25px;
  margin-left: 90%;
}

.serch_box{
  width: 70%;
  height: 20%;
  position: absolute;
  top:50%;
  left:50%;
    transform: translate(-50%, -50%);
  /* background: red; */
  display: flex;
    justify-content: center;
    align-items: center;

}
:deep(.el-input__inner){
      height: 46px;
}
.el-dropdown[data-v-0d0c7376] {

    /* margin-top: 11%; */
    margin-left: 84%;
}
.el-button {
  
    height: 44px;
}

</style>